<template>
  <div class="flight-logs-container">
    <div class="page-header">
      <h2>飞行日志管理</h2>
    </div>
    
    <!-- 搜索和筛选 -->
    <el-card class="search-card" shadow="hover">
      <el-form :model="searchForm" class="search-form">
        <el-form-item label="无人机ID">
          <el-input
            v-model="searchForm.drone_id"
            placeholder="请输入无人机ID"
            clearable
            size="small"
          />
        </el-form-item>
        <el-form-item label="任务ID">
          <el-input
            v-model="searchForm.task_id"
            placeholder="请输入任务ID"
            clearable
            size="small"
          />
        </el-form-item>
        <el-form-item label="仅显示错误">
          <el-switch
            v-model="searchForm.error_only"
            active-text="是"
            inactive-text="否"
            size="small"
          />
        </el-form-item>
        <el-form-item label="记录时间范围">
          <el-date-picker
            v-model="dateRange"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            size="small"
            value-format="YYYY-MM-DD HH:mm:ss"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSearch" size="small">
            <el-icon><component is="i-search" /></el-icon>
            搜索
          </el-button>
          <el-button @click="resetSearch" size="small">
            <el-icon><component is="i-refresh-left" /></el-icon>
            重置
          </el-button>
        </el-form-item>
      </el-form>
    </el-card>
    
    <!-- 日志表格 -->
    <el-card class="logs-table-card" shadow="hover">
      <el-table
        :data="logsData"
        style="width: 100%"
        border
        v-loading="loading"
        element-loading-text="加载中..."
      >
        <el-table-column prop="id" label="ID" width="80" fixed />
        <el-table-column prop="drone_id" label="无人机ID" width="100" />
        <el-table-column prop="drone_name" label="无人机名称" width="150" show-overflow-tooltip />
        <el-table-column prop="task_id" label="任务ID" width="100" />
        <el-table-column prop="task_name" label="任务名称" width="150" show-overflow-tooltip />
        <el-table-column label="位置信息" width="200">
          <template #default="{ row }">
            <div class="location-info">
              <div>经度: {{ row.longitude }}</div>
              <div>纬度: {{ row.latitude }}</div>
              <div>高度: {{ row.altitude }}m</div>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="飞行状态" width="150">
          <template #default="{ row }">
            <div class="flight-status">
              <div>速度: {{ row.speed }}m/s</div>
              <div>航向: {{ row.heading }}°</div>
              <div>模式: {{ row.flight_mode || '-' }}</div>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="电池状态" width="120">
          <template #default="{ row }">
            <div class="battery-info">
              <div>电压: {{ row.battery_voltage }}V</div>
              <div>电量: {{ row.battery_percentage || '-' }}%</div>
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="error_code" label="错误码" width="100" show-overflow-tooltip />
        <el-table-column prop="error_message" label="错误信息" width="200" show-overflow-tooltip />
        <el-table-column prop="record_time" label="记录时间" width="180" />
      </el-table>
      
      <!-- 分页 -->
      <div class="pagination-container">
        <el-pagination
          v-model:current-page="pagination.currentPage"
          v-model:page-size="pagination.pageSize"
          :page-sizes="[10, 20, 50, 100]"
          layout="total, sizes, prev, pager, next, jumper"
          :total="pagination.total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>
  </div>
</template>

<script>
import { ref, onMounted } from 'vue'
import axios from '../utils/request'
import { ElMessage } from 'element-plus'

export default {
  name: 'FlightLogs',
  setup() {
    const loading = ref(false)
    const searchForm = ref({
      drone_id: '',
      task_id: '',
      error_only: false
    })
    const dateRange = ref([])
    const pagination = ref({
      currentPage: 1,
      pageSize: 10,
      total: 0
    })
    const logsData = ref([])

    // 获取飞行日志数据
    const fetchLogs = async () => {
      loading.value = true
      try {
        const params = {
          page: pagination.value.currentPage,
          page_size: pagination.value.pageSize,
          drone_id: searchForm.value.drone_id,
          task_id: searchForm.value.task_id,
          error_only: searchForm.value.error_only
        }

        // 添加时间范围筛选
        if (dateRange.value && dateRange.value.length === 2) {
          params.start_time = dateRange.value[0]
          params.end_time = dateRange.value[1]
        }

        const response = await axios.get('/flight-logs', { params })
        if (response.code === 200) {
          logsData.value = response.data.items || []
          pagination.value.total = response.data.total || 0
        } else {
          ElMessage.error('获取飞行日志失败')
        }
      } catch (error) {
        ElMessage.error('获取飞行日志失败')
        console.error('获取飞行日志失败:', error)
      } finally {
        loading.value = false
      }
    }

    // 处理搜索
    const handleSearch = () => {
      pagination.value.currentPage = 1
      fetchLogs()
    }

    // 重置搜索
    const resetSearch = () => {
      searchForm.value.drone_id = ''
      searchForm.value.task_id = ''
      searchForm.value.error_only = false
      dateRange.value = []
      pagination.value.currentPage = 1
      fetchLogs()
    }

    // 处理分页大小变化
    const handleSizeChange = (size) => {
      pagination.value.pageSize = size
      fetchLogs()
    }

    // 处理页码变化
    const handleCurrentChange = (current) => {
      pagination.value.currentPage = current
      fetchLogs()
    }

    // 初始化
    onMounted(() => {
      fetchLogs()
    })

    return {
      loading,
      searchForm,
      dateRange,
      pagination,
      logsData,
      handleSearch,
      resetSearch,
      handleSizeChange,
      handleCurrentChange
    }
  }
}
</script>

<style scoped>
.flight-logs-container {
  padding: 20px;
}

.page-header {
  margin-bottom: 20px;
}

.search-card {
  margin-bottom: 20px;
}

.search-form {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-end;
  gap: 16px;
}

.logs-table-card {
  margin-bottom: 20px;
}

.pagination-container {
  display: flex;
  justify-content: flex-end;
  margin-top: 20px;
}

.location-info,
.flight-status,
.battery-info {
  font-size: 12px;
  line-height: 1.5;
}
</style>